﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace hmi
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static Window OptionWindow { get; set; }
        private CommManage cmma;

        public MainWindow()
        {
            InitializeComponent();            
        }

        private void FdBackEventInit()
        {
            //Protocol.Instance.InfFeedbackEvent += InfFeedback;
            Protocol.Instance.DataStateFdbackEvent += DataStateFdback;
            Protocol.Instance.SelfCheckStateFdbackEvent += SelfCheckStateFdback;
            //Protocol.Instance.GainFdbackEvent += GainFdback;
            Protocol.Instance.ViewFdbackEvent += ViewFdback;
            //Protocol.Instance.FocusFdbackEvent += FocusFdback;
            //Protocol.Instance.ContrastFdbackEvent += ContrastFdback;
            Protocol.Instance.OutModeFdbackEvent += OutModeFdback;
            Protocol.Instance.WorkModeFdbackEvent += WorkModeFdback;
            //Protocol.Instance.VideoRecordSateFdbackEvent += VideoRecordSateFdback;
            //Protocol.Instance.CrossModeFdbackEvent += CrossModeFdback;
            Protocol.Instance.TargetInfFdbackEvent += TargetInfFdback;
            CommManage.WrongEvent += WrongFdBack;//WrongEvent要先于CommManage.Instance创建,CommManage.Instance初始化的时候会检查当前通信是否正常，若异常，弹出提醒窗口
        }

        private void FdBackEventDestory()
        {
            //Protocol.Instance.InfFeedbackEvent -= InfFeedback;
            Protocol.Instance.DataStateFdbackEvent -= DataStateFdback;
            Protocol.Instance.SelfCheckStateFdbackEvent -= SelfCheckStateFdback;
            //Protocol.Instance.GainFdbackEvent -= GainFdback;
            Protocol.Instance.ViewFdbackEvent -= ViewFdback;
            //Protocol.Instance.FocusFdbackEvent -= FocusFdback;
            //Protocol.Instance.ContrastFdbackEvent -= ContrastFdback;
            Protocol.Instance.OutModeFdbackEvent -= OutModeFdback;
            Protocol.Instance.WorkModeFdbackEvent -= WorkModeFdback;
            //Protocol.Instance.VideoRecordSateFdbackEvent -= VideoRecordSateFdback;
            //Protocol.Instance.CrossModeFdbackEvent -= CrossModeFdback;
            Protocol.Instance.TargetInfFdbackEvent -= TargetInfFdback;
            CommManage.WrongEvent -= WrongFdBack;
        }

        private void OptionMenu_Click(object sender, RoutedEventArgs e)
        {            
            if (CommManage.Instance.IsUdp)
            {
                OptionWindow = UdpOptionWindow.CreateWindow();
            }
            else
            {
                OptionWindow = SerialOptionWindow.CreateWindow();                
            }
            OptionWindow.Owner = this;//赋予主窗口
            OptionWindow.Show();//ShowDialog方式打开窗口
        }

        private void DebugMenu_Click(object sender, RoutedEventArgs e)
        {
            DebugWindow window = DebugWindow.CreateWindow();
            window.Owner = this;//赋予主窗口
            window.Show();//ShowDialog方式打开窗口
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FdBackEventInit();
            //FDBackDisplayclear();
            cmma = CommManage.Instance;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            FdBackEventDestory();            
        }

        private void WrongFdBack()
        {
            MessageBox.Show("通信异常，请检查");
        }

        private void SelfCheckBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.SelfCheckStart();
        }

        private void GainAddBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.GainAdd();
        }

        private void GainDecBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.GainDec();
        }

        private void GainSetBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.GainSet(GainSlider.Value);
        }

        private void GainAutoBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.GainAuto();
        }

        private void FocusAddBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.ViewFieldAdd();
        }

        private void FocusDecBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.ViewFieldDec();
        }

        private void FocusSetBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.FocusSet(FocusSlider.Value);
        }

        private void FocusAutoBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.FocusAuto();
        }

        private void ContrastAddBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.ContrastAdd();
        }

        private void ContrastDecBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.ContrastDec();
        }

        private void ContrastSetBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.ContrastSet(ContrastSlider.Value);
        }

        private void ContrastAutoBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.ContrastAuto();
        }

        private void OriginalImageBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.OriginalImage();
        }

        private void EnhanceBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.Enhance();
        }

        private void SuperpositionBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.Superposition();
        }


        private void WorkModeBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.WorkMode();
        }

        private void StandbyModeBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.StandbyMode();
        }

        private void StartRecordBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.StartRecord();
        }

        private void StopRecordBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.StopRecord();
        }

        private void CrossLoadBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.CrossLoad();
        }

        private void CrossBlankBtn_Click(object sender, RoutedEventArgs e)
        {
            Protocol.Instance.CrossBlank();
        }

        private void DataStateFdback(string dataState)
        {
            Dispatcher.Invoke(() =>
            {
                DataStateFdBack.Text = dataState;
            });
        }

        private void SelfCheckStateFdback(string selfCheckState)
        {
            Dispatcher.Invoke(() =>
            {
                SelfCheckStateFdBackText.Text = selfCheckState;
            });
        }

        //private void GainFdback(string gain)
        //{
        //    Dispatcher.Invoke(() =>
        //    {
        //        GainFdBackText.Text = gain;
        //    });
        //}

        private void ViewFdback(string gain)
        {
            Dispatcher.Invoke(() =>
            {
                ViewFdBackText.Text = gain;
            });
        }



        //private void FocusFdback(string focus)
        //{
        //    Dispatcher.Invoke(() =>
        //    {

        //        FocusFdBackText.Text = focus;
        //    });
        //}

        //private void ContrastFdback(string contrast)
        //{
        //    Dispatcher.Invoke(() =>
        //    {
        //        ContrastFdBackText.Text = contrast;
        //    });
        //}

        private void OutModeFdback(string outMode)
        {
            Dispatcher.Invoke(() =>
            {
                OutModeFdBackText.Text = outMode;
            });
        }

        private void WorkModeFdback(string workMode)
        {
            Dispatcher.Invoke(() =>
            {
                WorkModeFdBackText.Text = workMode;
            });
        }

        //private void VideoRecordSateFdback(string videoRecordSate)
        //{
        //    Dispatcher.Invoke(() =>
        //    {
        //        RecordFdBackText.Text = videoRecordSate;
        //    });
        //}

        //private void CrossModeFdback(string crossMode)
        //{
        //    Dispatcher.Invoke(() =>
        //    {
        //        CrossFdBackText.Text = crossMode;
        //    });
        //}

        private void TargetInfFdback(string targetInf)
        {
            Dispatcher.Invoke(() =>
            {
                TargetInfText.Text = targetInf;
            });
        }

        //private void InfFeedback(string dataState, string selfCheckState, string view, string outMode, string workMode, string targetInf)
        //{
        //    Dispatcher.Invoke(() =>
        //    {
        //        DataStateFdBack.Text = dataState;
        //        SelfCheckStateFdBackText.Text = selfCheckState;
        //        ViewFdBackText.Text = view;
        //        OutModeFdBackText.Text = outMode;
        //        WorkModeFdBackText.Text = workMode;
        //        TargetInfText.Text = targetInf;
        //    });
        //}
    }
}
